//
//  BankListViewController.swift
//  Order
//
//  Created by Monster . on 2020/7/4.
//  Copyright © 2020 lazyloading. All rights reserved.
//

import UIKit
import PKHUD
import MGSwipeTableCell

let kCellIdentifier = "com.bank.list.cell.identifier"


class BankListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MGSwipeTableCellDelegate{
    @IBOutlet weak var tableView: UITableView!
    
    var dataArr: [BankListModel] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "银行卡";
  
        self.setupSubviewsUI();
     
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        self.listDataRequest();
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80;
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataArr.count+1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if dataArr.count == indexPath.row {
            let cell = tableView.dequeueReusableCell(withIdentifier: "add");
            return cell!
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellIdentifier) as! BankListCell;
        let model : BankListModel  = self.dataArr[indexPath.row] ;
        cell.titleLabel.text = model.cardName;
        cell.detailLabel.text = model.cardNum;
        cell.delegate = self;
        
        weak var weakSelf = self;
         let button = MGSwipeButton(title: "删除", backgroundColor: .red) { (cell : MGSwipeTableCell) -> Bool in
            weakSelf?.deleteCardrRequest(model: model);
            return true;
        }
        button.frame = CGRect.init(x: 0, y: 0, width: 90, height: 80);
        cell.rightButtons = [button];

        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if dataArr.count == indexPath.row {
            self.navigationController?.pushViewController(AddBankController(), animated: true);
        }
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if dataArr.count == indexPath.row {
            return false;
        }
        return true;
    }
    
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete;
    }

    func swipeTableCell(_ cell: MGSwipeTableCell, canSwipe direction: MGSwipeDirection, from point: CGPoint) -> Bool {
        if direction == .rightToLeft {
            return true;
        }
        return false;
    }
//    func swipeTableCell(_ cell: MGSwipeTableCell, swipeButtonsFor direction: MGSwipeDirection, swipeSettings: MGSwipeSettings, expansionSettings: MGSwipeExpansionSettings) -> [UIView]? {
//        let button = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 90, height: 80))
//        button.setTitle("删除", for: .normal);
//        button.titleLabel?.font = UIFont.init(fontType: .regular, fontSize: 16);
//        button.backgroundColor = .red;
//        button.addTarget(self, action: #selector(deleteCardrRequest), for: .touchUpInside);
//        return [button];
//    }
    
    //列表数据
    func listDataRequest()  {
        let param = ["" : ""];
        
        MoyaRequest(.bankList(param), success: { (response : BaseResponse<[BankListModel]>) in
            if response.respCode == 200{
                self.dataArr = response.data!;
                self.tableView.reloadData();
            }else{
                HUD.flash(.label(response.respMessage), delay: 1);
            }
        }) { (error) in
            HUD.flash(.label("请求失败"), delay: 1);
        };
        
    }
    
     func deleteCardrRequest( model : BankListModel)  {
        weak var weakSelf = self;
        let param = [
            "id" : model.id ?? ""
        ];
        MoyaRequest(.deleteBankCard(param), success: { (response : BaseResponse<[String:Any]>) in
            if response.respCode == 200 {
                weakSelf?.listDataRequest();
            }else{
                HUD.flash(.label(response.respMessage), delay: 1);
            }
        }) { (error) in
        
            HUD.flash(.label("删除失败，请稍后再试"), delay: 1);
        };
    }
    
    func setupSubviewsUI() {
        tableView.backgroundColor = UIColor.groupTableViewBackground
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.registerNib("BankListCell", kCellIdentifier)
        tableView.registerNib("BankListAddCell", "add")
    }
}
